package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.SysUserRoleDao;
import com.sugon.entity.SysRoleEntity;
import com.sugon.entity.SysUserEntity;
import com.sugon.modules.sys.consts.SysDeptConst;
import com.sugon.modules.sys.consts.SysPerms;
import com.sugon.service.SysUserRoleService;
import com.sugon.utils.ProcessInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * 用户与角色对应关系
 *
 * @author lipengjun
 * @date 2016年9月18日 上午9:45:48
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl implements SysUserRoleService {
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public void saveOrUpdate(String userId, List<String> roleIdList) {
        if (roleIdList.size() == 0) {
            return;
        }

        //先删除用户与角色关系
        sysUserRoleDao.delete(userId);

        //保存用户与角色关系
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);

        List newroleList = new ArrayList();
        for (int i = 0; i < roleIdList.size(); i++) {
            String roleId = roleIdList.get(i);
            String uuId = RandomUtil.randomString(32);
            Map mapList = new HashMap();
            mapList.put("roldId", roleId);
            mapList.put("uuId", uuId);
            newroleList.add(mapList);
        }
        map.put("roleIdList", newroleList);
        sysUserRoleDao.save(map);
    }

    @Override
    public List<String> queryRoleIdList(String userId) {
        return sysUserRoleDao.queryRoleIdList(userId);
    }

    @Override
    public void delete(String userId) {
        sysUserRoleDao.delete(userId);
    }

    @Override
    public List<SysUserEntity> queryDeptRoleUserList(String deptId, String roleId) {
        if (StrUtil.isNotEmpty(deptId)) {
            // 先尝试从车间内查询角色，如果没有则从制造处查询
            List<SysUserEntity> workshopRoleUserList = sysUserRoleDao.queryWorkshopRoleUser(deptId, roleId);
            if (CollectionUtil.isEmpty(workshopRoleUserList)) {
                workshopRoleUserList = sysUserRoleDao.queryWorkshopRoleUser(SysDeptConst.ZZC_CODE, roleId);
            }
            return workshopRoleUserList;
        } else {
            return sysUserRoleDao.queryWorkshopRoleUser(SysDeptConst.ZZC_CODE, roleId);
        }
    }

    @Override
    public List<SysUserEntity> queryDeptUserByRoleId(String deptId, String... roleId) {
        List<String> roleIdList = CollectionUtil.newArrayList(roleId);
        List<SysUserEntity> userList = sysUserRoleDao.queryUser(deptId, roleIdList);
        return Optional.ofNullable(userList).orElse(CollectionUtil.newArrayList());
    }

    @Override
    public List<SysUserEntity> queryRoleUserList(List<SysRoleEntity> roleList) {
        List<SysUserEntity> roleUserList = CollectionUtil.newArrayList();
        if (ArrayUtil.isEmpty(roleList)) {
            return roleUserList;
        }
        roleUserList = sysUserRoleDao.queryUserByRoleIdList(roleList.stream().map(SysRoleEntity::getRoleId).collect(Collectors.toList()));
        return Optional.ofNullable(roleUserList).orElseGet(() -> CollectionUtil.newArrayList());
    }

    @Override
    public List<Map<String, Object>> queryDeptMenuUserList(String deptId, String menuId) {
        if (StrUtil.isNotEmpty(deptId)) {
            // 先尝试从车间内查询角色，如果没有则从制造处查询
            List<Map<String, Object>> workshopRoleUserList = sysUserRoleDao.queryWorkshopMenuUser(deptId, menuId);
            if (CollectionUtil.isEmpty(workshopRoleUserList)) {
                workshopRoleUserList = sysUserRoleDao.queryWorkshopMenuUser(SysDeptConst.ZZC_CODE, menuId);
            }
            return workshopRoleUserList;
        } else {
            return sysUserRoleDao.queryWorkshopMenuUser(SysDeptConst.ZZC_CODE, menuId);
        }
    }

    @Override
    public List<String> queryRoleNamesByProcessItem(ProcessInfoEnum processItem, String userId) {
        List<String> roleNames = null;
        SysPerms[] perms = processItem.getSysPerms();
        if (ArrayUtil.isNotEmpty(perms)) {
            List<String> menuIdList = Arrays.stream(perms).map(SysPerms::getMenuId).collect(Collectors.toList());
            roleNames = sysUserRoleDao.queryRoleNames(userId, menuIdList);
        }
        if (roleNames == null) {
            roleNames = new ArrayList<>();
        }
        return roleNames;
    }
}
